package com.clsz.controller;

import com.clsz.bean.bo.UserRegistBo;
import com.clsz.bean.po.UserInfo;
import com.clsz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录注册
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Object register(UserRegistBo userRegistBo) {
        Map<String, Object> res = new HashMap<>();
        int result = userService.regist(userRegistBo);
        if (result == -1) {
            res.put("code", "501");
            res.put("message", "验证码校验失败");
        } else if (result == -2) {
            res.put("code", "502");
            res.put("message", "用户已存在！！！");
        } else {
            res.put("code", "200");
            res.put("message", "ok!!!");
        }
        return res;
    }

    @PostMapping("/login")
    public Object login(String username, String password) {
        System.out.println(username);
        Map<String, Object> res = new HashMap<>();
        String token = userService.login(username, password);
        if (token == null) {
            res.put("code", "401");
            res.put("message", "Unauthorized");
        } else {
            res.put("code", "200");
            res.put("message", "OK!!!");
            res.put("data", token);
        }
        return res;
    }
}
